package cn.iocoder.yudao.module.biz.job.planExe;

import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler;
import cn.iocoder.yudao.framework.tenant.core.job.TenantJob;
import cn.iocoder.yudao.module.biz.controller.admin.htPlan.vo.HtPlanUpdateReqVO;
import cn.iocoder.yudao.module.biz.controller.admin.htPlanExe.vo.HtPlanExeCreateReqVO;
import cn.iocoder.yudao.module.biz.service.htPlan.HtPlanService;
import cn.iocoder.yudao.module.biz.service.htPlanExe.HtPlanExeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 自动生成排查计划待执行任务
 */
@Component
@Slf4j
@TenantJob
public class HtPlanExeJob implements JobHandler {
    @Resource
    private HtPlanService planService;
    @Resource
    private HtPlanExeService planExeService;

    private DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    private DateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd 00:00:00");

    @Override
    public String execute(String param) throws Exception {
        //查询排查计划:待执行、执行中，明日是否有排查任务
        Map<String, Object> map = new HashMap<String, Object>();
        String today=dateFormat.format(new Date());
        map.put("today",today );

        //本次排查开始时间 、班次排查结束时间
        List<HtPlanExeJoinVO> listPlan = planService.listPlanExe(map);
        //根据排查计划，生成排查执行任务
        for (HtPlanExeJoinVO planVO : listPlan) {
            Calendar cal = Calendar.getInstance();
            String todayTime = new SimpleDateFormat("yyyy-MM-dd 00:00:00").format(cal.getTime());

            long d1 = timeFormat.parse(todayTime).getTime();
            long d2 = planVO.getEndDate().getTime();
            //修改排查计划的状态：起始日期相同，修改为已执行；否则不做修改
            if(d1==d2) {
                HtPlanUpdateReqVO updateReqVO=new HtPlanUpdateReqVO();
                updateReqVO.setId(planVO.getPlanId());
                updateReqVO.setPlanState("1");
                planService.updateHtPlan(updateReqVO);
            }

            String chekcPeriod = planVO.getCheckPeriod();

            HtPlanExeCreateReqVO createReqVO = new HtPlanExeCreateReqVO();
            createReqVO.setPlanId(planVO.getPlanId());
            createReqVO.setStartTime(timeFormat.parse(todayTime));
            createReqVO.setExeState("0");
            createReqVO.setUserId(planVO.getCheckPeoId());

            Map<String, Object> params = new HashMap<>();
            params.put("planId", planVO.getPlanId());
            boolean isExist = true;

            Calendar cal2 = Calendar.getInstance();

            switch (chekcPeriod) {
                case "1": //插入新的记录（起始日期：当前日期、当前日期+1）
                    //判断是否有重复的记录
                    cal2.add(Calendar.DATE,-1);
                    params.put("startTime", cal2.getTime());
                    isExist = planExeService.isPlanExeExist(params);
                    if (!isExist) {
                        cal.add(Calendar.DATE, 1);
                        String tomorrow = new SimpleDateFormat("yyyy-MM-dd 00:00:00").format(cal.getTime());
                        createReqVO.setEndTime(timeFormat.parse(tomorrow));
                        planExeService.createHtPlanExe(createReqVO);
                    }
                    break;
                case "2"://一周一次
                    cal2.add(Calendar.DATE,-7);
                    params.put("startTime", cal2.getTime());
                    isExist = planExeService.isPlanExeExist(params);
                    if (!isExist) {
                        cal.add(Calendar.DATE, 7);
                        String day7 = new SimpleDateFormat("yyyy-MM-dd 00:00:00").format(cal.getTime());
                        if(planVO.getEndDate().compareTo(cal.getTime())<0){
                            createReqVO.setEndTime(planVO.getEndDate());
                        }else{
                            createReqVO.setEndTime(timeFormat.parse(day7));
                        }
                        planExeService.createHtPlanExe(createReqVO);
                    }
                    break;
                case "3"://一月一次
                    cal2.add(Calendar.MONTH, -1);
                    params.put("startTime", cal2.getTime());
                    isExist = planExeService.isPlanExeExist(params);
                    if (!isExist) {
                        cal.add(Calendar.MONTH, 1);
                        String month1 = new SimpleDateFormat("yyyy-MM-dd 00:00:00").format(cal.getTime());
                        if(planVO.getEndDate().compareTo(cal.getTime())<0){
                            createReqVO.setEndTime(planVO.getEndDate());
                        }else{
                            createReqVO.setEndTime(timeFormat.parse(month1));
                        }
                        planExeService.createHtPlanExe(createReqVO);
                    }
                    break;
                case "4"://三月一次
                    cal2.add(Calendar.MONTH, -3);
                    params.put("startTime", cal2.getTime());
                    isExist = planExeService.isPlanExeExist(params);
                    if (!isExist) {
                        cal.add(Calendar.MONTH, 3);
                        String month3 = new SimpleDateFormat("yyyy-MM-dd 00:00:00").format(cal.getTime());
                        if(planVO.getEndDate().compareTo(cal.getTime())<0){
                            createReqVO.setEndTime(planVO.getEndDate());
                        }else{
                            createReqVO.setEndTime(timeFormat.parse(month3));
                        }
                        planExeService.createHtPlanExe(createReqVO);
                    }
                    break;
                case "5"://6月一次
                    cal2.add(Calendar.MONTH, -6);
                    params.put("startTime", cal2.getTime());
                    isExist = planExeService.isPlanExeExist(params);
                    if (!isExist) {
                        cal.add(Calendar.MONTH, 6);
                        String month6 = new SimpleDateFormat("yyyy-MM-dd 00:00:00").format(cal.getTime());
                        if(planVO.getEndDate().compareTo(cal.getTime())<0){
                            createReqVO.setEndTime(planVO.getEndDate());
                        }else{
                            createReqVO.setEndTime(timeFormat.parse(month6));
                        }
                        planExeService.createHtPlanExe(createReqVO);
                    }
                    break;
                case "6"://12月一次
                    cal2.add(Calendar.MONTH, -12);
                    params.put("startTime", cal2.getTime());
                    isExist = planExeService.isPlanExeExist(params);
                    if (!isExist) {
                        cal.add(Calendar.MONTH, 12);
                        String month12 = new SimpleDateFormat("yyyy-MM-dd 00:00:00").format(cal.getTime());
                        if(planVO.getEndDate().compareTo(cal.getTime())<0){
                            createReqVO.setEndTime(planVO.getEndDate());
                        }else{
                            createReqVO.setEndTime(timeFormat.parse(month12));
                        }
                        planExeService.createHtPlanExe(createReqVO);
                    }
                    break;
                default:
                    throw new Exception("没有找到匹配的排查周期！");
            }
        }
        return "执行完毕！";
    }
}
